﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Runtime.Serialization;
using System.Threading.Tasks;
using System.Web;
using System.Net.Http;

namespace LynxWebLog
{
    [DataContract(Namespace = "http://schemas.datacontract.org/2004/07/LynxBlogAPI.Models")]
    public class CategoryData
    {
        private int _linkCnt = 0;

        [DataMember]
        public int categoryid { get; set; }
        [DataMember, Required]
        public string username { get; set; }
        [DataMember]
        public string title { get; set; }
        [DataMember]
        public string description { get; set; }
        [DataMember]
        public string htmlUrl { get; set; }
        [DataMember]
        public string rssUrl { get; set; }
        [IgnoreDataMember]
        public int linkCnt {
            get { return _linkCnt; }
            set { _linkCnt = value; }
        }

        public CategoryData() { }
        public CategoryData(CategoryInfo ci, string username)
        {
            this.categoryid = ci.categoryid;
            this.username = username;
            this.title = ci.title;
            this.description = ci.description;
            this.htmlUrl = ci.htmlUrl;
            this.rssUrl = ci.rssUrl;
        }

        public async Task<int> GetLinkCnt(int catId)
        {
            int cnt = 0;
            HttpClient client = ClientUtility.BlogAPIClient();
            HttpResponseMessage resp = await client.GetAsync("api/PostCategory/?$filter=categoryid eg " + catId);
            resp.EnsureSuccessStatusCode();

            cnt = (await resp.Content.ReadAsAsync<IEnumerable<PostCategory>>()).Count();

            return cnt;
        }

    }
}